import Vue from 'vue';
import Router from 'vue-router';
import { BasicLayout } from '@/layouts'
Vue.use(Router);

const routes = [
  {
    path: '*',
    redirect: '/home'
  },
  {
    name: 'home',
    path: "/home",
    component: BasicLayout,
    redirect: '/home/index',
    meta: {
      title: '首页'
    },
    children: [
      {
        name: 'user',
        path: "/home/user",
        component: () => import('./view/user'),
        meta: {
          title: '会员中心'
        }
      },
      {
        name: 'cart',
        path: "/home/cart",
        component: () => import('./view/cart'),
        meta: {
          title: '购物车'
        }
      },
      {
        name: 'index',
        path: "/home/index",
        component: () => import('./view/home'),
        meta: {
          title: '首页'
        }
      },
    ]
  },
  {
    name: 'goods',
    component: () => import('./view/goods'),
    meta: {
      title: '商品详情'
    }
  }, {
    name: 'order',
    component: () => import('./view/order'),
    meta: {
      title: '确认订单'
    }
  },
  {
    name: 'payResult',
    component: () => import('./view/payResult'),
    meta: {
      title: '支付结果'
    }
  },
  {
    name: 'login',
    component: () => import('./view/user/login'),
    meta: {
      title: '登录'
    }
  },
  {
    name: 'register',
    component: () => import('./view/user/register'),
    meta: {
      title: '注册'
    }
  }

];

// add route path
routes.forEach(route => {
  route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  next();
});

export {
  router
};
